﻿/* 
 * Copyright (c) 2010 Letwory Interactive
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -- Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * -- Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * Original contributor: Nathan Letwory
 */

using System;

namespace VerseSharp
{
    public delegate void TagCreateCallback(TagCreateCommand command);
    public delegate void TagDestroyCallback(TagCreateCommand command);

    public struct vanimation
    {
        public uint curve;
        public uint start;
        public uint end;
    };

    public struct vblob
    {
        public ushort size;
        public byte[] blob;
    };

    public struct Tag
    {
        public bool vboolean;
        public uint vuint32;
        public double vreal64;
        public string vstring;
        public double[] vreal64_vec3;
        public uint vlink;
        public vanimation vanimation;
        public vblob vblob;
    }

    public class TagCreateCommand: IVerseCommand
    {
        /// <summary>
        /// Command ID
        /// </summary>
        public CommandID ID { get { return CommandID.TagCreate; } }
        /// <summary>
        /// ID of the node being accessed.
        /// </summary>
        public uint NodeID;
        /// <summary>
        /// ID of the group being accessed. To create a new group, set it to ushort.MaxValue.
        /// Setting to a known ID means renaming of the tag group.
        /// </summary>
        public ushort GroupID;
        /// <summary>
        /// ID of the tag being accessed. To create a new tag, set it to ushort.MaxValue.
        /// Setting to a known ID means renaming of the tag.
        /// </summary>
        public ushort TagID;
        private string _name;
        /// <summary>
        /// Name of the tag. Truncated to 16 characters long. Set to null or empty if you
        /// want this command to destroy the specified tag group.
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                if (value != null)
                {
                    if (value.Length <= 16)
                    {
                        _name = value;
                    }
                    else
                    {
                        _name = value.Substring(0, 16);
                    }
                }
            }
        }

        public TagType Type;
        public Tag Tag;

        public void Deserialize(byte[] source, ref int pos)
        {
            NodeID = Utils.GetUInt32(source, ref pos);
            GroupID = Utils.GetUInt16(source, ref pos);
            TagID = Utils.GetUInt16(source, ref pos);
            _name = Utils.GetString(source, 16, ref pos);
            Type = (TagType)Utils.GetUInt8(source, ref pos);
            switch (Type)
            {
                case TagType.Boolean:
                    Tag.vboolean = Utils.GetUInt8(source, ref pos)!=0;
                    break;
                case TagType.UInt32:
                    Tag.vuint32 = Utils.GetUInt32(source, ref pos);
                    break;
                case TagType.Real64:
                    Tag.vreal64 = Utils.GetReal64(source, ref pos);
                    break;
                case TagType.String:
                    Tag.vstring = Utils.GetString(source, 500, ref pos);
                    break;
                case TagType.Real64Vec3:
                    Tag.vreal64_vec3 = new double[3];
                    Tag.vreal64_vec3[0] = Utils.GetReal64(source, ref pos);
                    Tag.vreal64_vec3[1] = Utils.GetReal64(source, ref pos);
                    Tag.vreal64_vec3[2] = Utils.GetReal64(source, ref pos);
                    break;
                case TagType.Link:
                    Tag.vlink = Utils.GetUInt32(source, ref pos);
                    break;
                case TagType.Animation:
                    Tag.vanimation.curve = Utils.GetUInt32(source, ref pos);
                    Tag.vanimation.start = Utils.GetUInt32(source, ref pos);
                    Tag.vanimation.end = Utils.GetUInt32(source, ref pos);
                    break;
                case TagType.Blob:
                    Tag.vblob.size = Utils.GetUInt16(source, ref pos);
                    Tag.vblob.blob = new byte[Tag.vblob.size];
                    Buffer.BlockCopy(source, pos, Tag.vblob.blob, 0, Tag.vblob.size);
                    pos += Tag.vblob.size;
                    break;
            }
        }

        public void Serialize(byte[] destination, ref int pos)
        {
            Utils.PutUInt32(destination, ref pos, NodeID);
            Utils.PutUInt16(destination, ref pos, GroupID);
            Utils.PutUInt16(destination, ref pos, TagID);
            Utils.PutString(destination, _name, 16, ref pos);
            Utils.PutUInt8(destination, ref pos, (byte)Type);
            switch (Type)
            {
                case TagType.Boolean:
                    Utils.PutUInt8(destination, ref pos, (byte)(Tag.vboolean ? 1 : 0));
                    break;
                case TagType.UInt32:
                    Utils.PutUInt32(destination, ref pos, Tag.vuint32);
                    break;
                case TagType.Real64:
                    Utils.PutReal64(destination, ref pos, Tag.vreal64);
                    break;
                case TagType.String:
                    Utils.PutString(destination, Tag.vstring, 500, ref pos);
                    break;
                case TagType.Real64Vec3:
                    Utils.PutReal64(destination, ref pos, Tag.vreal64_vec3[0]);
                    Utils.PutReal64(destination, ref pos, Tag.vreal64_vec3[1]);
                    Utils.PutReal64(destination, ref pos, Tag.vreal64_vec3[2]);
                    break;
                case TagType.Link:
                    Utils.PutUInt32(destination, ref pos, Tag.vlink);
                    break;
                case TagType.Animation:
                    Utils.PutUInt32(destination, ref pos, Tag.vanimation.curve);
                    Utils.PutUInt32(destination, ref pos, Tag.vanimation.start);
                    Utils.PutUInt32(destination, ref pos, Tag.vanimation.end);
                    break;
                case TagType.Blob:
                    Utils.PutUInt16(destination, ref pos, Tag.vblob.size);
                    Buffer.BlockCopy(Tag.vblob.blob, 0, destination, pos, Tag.vblob.size);
                    pos += Tag.vblob.size;
                    break;
            }
        }
    }
}
